//
//  DserXiangqingVC.m
//  Tour
//
//  Created by 张张烨 on 16/3/12.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "DserXiangqingVC.h"
#import "DserXiangqingView.h"

#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <QuartzCore/QuartzCore.h>
#import <TencentOpenAPI/TencentOAuth.h>

#define screenFrame [UIScreen mainScreen].bounds
@interface DserXiangqingVC ()<TencentSessionDelegate,TencentLoginDelegate>
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,assign)CGRect cutFrame;
@property(nonatomic,assign)CGPoint cent;
@property(nonatomic,strong)NSDictionary *playInfoDic;
@property(nonatomic,strong)UILabel *shoucangLabel;
@property(nonatomic,assign)BOOL isShoucang;
@property(nonatomic,assign)BOOL isShared;
@property(nonatomic,assign)double shoucangCount;
@property(nonatomic,strong)UIButton *shoucangButton;
@property (nonatomic, strong)TencentOAuth *tencentOAuth;/**< qq */
@property(nonatomic,assign)double sharedCount;
@property(nonatomic,strong)UIImage *shoucangImage;
@end

@implementation DserXiangqingVC
-(void)loadView{
    [super loadView];
    
    [self allFrame];
    self.view.backgroundColor = [UIColor blackColor];
}
-(void)viewWillAppear:(BOOL)animated
{
    [[DataKu shareInstance] openDb];
    [[DataKu shareInstance] creteTable];
    
    NSArray *arr = [[DataKu shareInstance] selet];
    
    DakuKuLei * aaa = [[DakuKuLei alloc]init];
    if (arr.count) {
        aaa.mingzi = _modle1111.sanModel.title;
        for (DakuKuLei * m in arr) {
            if ([aaa.mingzi isEqualToString:m.mingzi]) {
                _shoucangLabel.text = [NSString stringWithFormat:@"以成功"];
                _shoucangImage = [UIImage imageNamed:@"已收藏"];
                [_shoucangButton setBackgroundImage:_shoucangImage forState:UIControlStateNormal];
               break;
            }else{
                _shoucangLabel.text = [NSString stringWithFormat:@"未收藏"];
                _shoucangImage = [UIImage imageNamed:@"未收藏"];
                [_shoucangButton setBackgroundImage:_shoucangImage forState:UIControlStateNormal];
            }
            
        }

    }else{
        _shoucangLabel.text = [NSString stringWithFormat:@"未收藏"];
        _shoucangImage = [UIImage imageNamed:@"未收藏"];
        [_shoucangButton setBackgroundImage:_shoucangImage forState:UIControlStateNormal];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _isShared = 0;
    _isShoucang = 0;
    self.navigationItem.title = _modle1111.sanModel.title;
//    [self anniu];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)anniu
{
    UIBarButtonItem *rigthItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"QQ分享"] style:(UIBarButtonItemStylePlain) target:self action:@selector(you)];
     UIBarButtonItem *rigthItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"QQ空间"] style:(UIBarButtonItemStylePlain) target:self action:@selector(kongjian)];
    self.navigationItem.rightBarButtonItems = @[rigthItem,rigthItem1];
}
- (void)kongjian{
    QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:@"www.baidu.com"] title:@"张先生" description:@"aaaaa" previewImageURL:[NSURL URLWithString:@"www.baidu.com"] targetContentType:QQApiURLTargetTypeNews];
    
    
    
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
//    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    //将内容分享到qzone
    QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    
    [self handleSendResult:sent];
}

- (void)you
{
    QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:@"www.baidu.com"] title:@"张先生" description:@"aaaaa" previewImageURL:[NSURL URLWithString:@"www.baidu.com"] targetContentType:QQApiURLTargetTypeNews];
    
    
    
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    //将内容分享到qzone
//        QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    
    [self handleSendResult:sent];
    
    
    
}
- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
        case EQQAPIAPPNOTREGISTED:
        {
            [self ceshi];
            
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        default:
        {
            break;
        }
    }
}
- (void)ceshi
{
    
    self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105183015" andDelegate:self];
    
    NSArray* permissions = [NSArray arrayWithObjects:
                            kOPEN_PERMISSION_GET_USER_INFO,
                            kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
                            kOPEN_PERMISSION_ADD_ALBUM,
                            kOPEN_PERMISSION_ADD_ONE_BLOG,
                            kOPEN_PERMISSION_ADD_SHARE,
                            kOPEN_PERMISSION_ADD_TOPIC,
                            kOPEN_PERMISSION_CHECK_PAGE_FANS,
                            kOPEN_PERMISSION_GET_INFO,
                            kOPEN_PERMISSION_GET_OTHER_INFO,
                            kOPEN_PERMISSION_LIST_ALBUM,
                            kOPEN_PERMISSION_UPLOAD_PIC,
                            kOPEN_PERMISSION_GET_VIP_INFO,
                            kOPEN_PERMISSION_GET_VIP_RICH_INFO,
                            nil];
    
    [_tencentOAuth authorize:permissions inSafari:NO];
    
    [_tencentOAuth setAccessToken:_tencentOAuth.accessToken];
    [_tencentOAuth setOpenId:_tencentOAuth.openId];
    [_tencentOAuth setExpirationDate:_tencentOAuth.expirationDate];
    
    
}
-(void)allFrame{
    //高斯效果视图
    UIImageView *blurimage  =[[UIImageView alloc] initWithFrame:CGRectMake( 0, screenFrame.size.height - (330 * GAO ), screenFrame.size.width, 300 * GAO)];
    [self.view addSubview:blurimage];
    
    UIView *blurView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenFrame.size.width, 300 * GAO)];
    blurimage.userInteractionEnabled = YES;
    blurView.backgroundColor = [UIColor blackColor];
    blurView.alpha = 0.65;
    [blurimage addSubview:blurView];
    //视频视图
    UIImageView *MVview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenFrame.size.width, screenFrame.size.height - (260 * GAO) )];
    //按比例调整大小 UIImage 然后裁剪
    MVview.clipsToBounds = YES;
    [MVview setContentScaleFactor:[[UIScreen mainScreen] scale]];
    MVview.contentMode =  UIViewContentModeScaleAspectFill;
    MVview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
//    MVview.frame = CGRectMake(0, 0, screenFrame.size.width, 160*screenFrame.size.width/320);
    MVview.backgroundColor = [UIColor blackColor];
    [self.view addSubview:MVview];
    //播放按钮视图
    UIView *bofangButtonView = [[UIView alloc] init];
    bofangButtonView.backgroundColor = [UIColor clearColor];
    [MVview addSubview:bofangButtonView];
    //收藏button
    _shoucangButton = [[UIButton alloc] initWithFrame:CGRectMake(10 * KUAN , 150 * GAO, 25 * KUAN, 25 * GAO)];
    _shoucangImage = [UIImage imageNamed:@"未收藏.png"];
    [_shoucangButton setBackgroundImage:_shoucangImage forState:UIControlStateNormal];
    [_shoucangButton addTarget:self action:@selector(shoucanButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_shoucangButton setShowsTouchWhenHighlighted:1];
    _sharedCount = 1;
    
    [blurView addSubview:_shoucangButton];

    //收藏label
    _shoucangLabel = [[UILabel alloc] initWithFrame:CGRectMake(45 * KUAN, 150 * GAO, 65 * KUAN, 25 * GAO)];
    _shoucangLabel.font = [UIFont systemFontOfSize:13];
    _shoucangLabel.textColor = [UIColor whiteColor];
    _shoucangCount = [self.playInfoDic[@"collectionCount"] doubleValue];
    _shoucangCount = 0;
    _shoucangLabel.text = [NSString stringWithFormat:@"未收藏"];
    [blurView addSubview:_shoucangLabel];
    
    //播放button
    UIButton *bofangButton = [[UIButton alloc] init];
    bofangButton.frame = CGRectMake(0, 0, screenFrame.size.width, screenFrame.size.height - (264 * GAO));
    bofangButton.center = MVview.center;
    [bofangButton setBackgroundImage:[UIImage imageNamed:@"播放.png"]  forState:UIControlStateNormal];
        [bofangButton setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
        bofangButton.backgroundColor = [UIColor clearColor];
    UIImage *oii = [UIImage imageNamed:@"播放大.png"];
    CGSize size = CGSizeMake(64 * KUAN, 64 * GAO);
    UIImageView *bofangImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    bofangImage.image = oii;
    bofangImage.center = CGPointMake(screenFrame.size.width/2, (screenFrame.size.height - (264 * GAO ))/2);
    [bofangButton addSubview:bofangImage];
    [bofangButton addTarget:self action:@selector(bofangButtonBackGroundHighlighted:) forControlEvents:UIControlEventTouchUpInside];
    
    //[bofangButtonView addSubview:bofangButton];
    [self.view addSubview:bofangButton];
    //视频主题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * KUAN, 5 * GAO, screenFrame.size.width - (10 * KUAN ), 30 * GAO )];
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    titleLabel.textColor = [UIColor whiteColor];
    //titleLabel.text = @"哈哈";
    [blurView addSubview:titleLabel];
    //分割线
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 35 * GAO, screenFrame.size.width - (120 * KUAN), 1)];
    line.backgroundColor = [UIColor whiteColor];
    [blurView addSubview:line];
    
    //种类label
    UILabel *kindLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * KUAN, 40 * GAO, 45 * KUAN, 25 * GAO)];
    kindLabel.text = @"#种类";
    kindLabel.font = [UIFont systemFontOfSize:14];
    kindLabel.textColor = [UIColor whiteColor];
    [blurView addSubview: kindLabel];
    //侧分割线
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * KUAN, 40 * GAO, 5 * KUAN, 25 * GAO)];
    lineLabel.text = @"/";
    lineLabel.font = [UIFont systemFontOfSize:14];
    lineLabel.textColor = [UIColor whiteColor];
    [blurView addSubview:lineLabel];
    //时间label
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(65 * KUAN, 40 * GAO, 45 * KUAN, 25 * GAO)];
    timeLabel.text = @"00'00\"";
    timeLabel.font = [UIFont systemFontOfSize:14];
    timeLabel.textColor = [UIColor whiteColor];
    [blurView addSubview:timeLabel];
    //描述label
    UILabel *detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(5 * KUAN, 70 * GAO, screenFrame.size.width - (10 * KUAN ), 65 * GAO)];
    detailLabel.numberOfLines = 0;
    detailLabel.font = [UIFont systemFontOfSize:13];
    //    detailLabel.text = @"asdasdasdasdadasdasgdfgsgsfsdfsdgdadfgbsdffsbddggdsdfdhsdfvdbhfghsfgdfhfghsfdgdhdfgfdssshfngfdkasuhdkhsdckhakusdhaiuhgiusdhgfishifghsidfhihsfishgdfiahgdifghsidufgisu";
    detailLabel.backgroundColor = [UIColor blackColor];
    detailLabel.textColor = [UIColor grayColor];
    //上传button
    UIButton *shangchuanButton = [[UIButton alloc] initWithFrame:CGRectMake(120 * KUAN, 150 * GAO, 25 * KUAN, 25 * GAO)];
    [shangchuanButton setBackgroundImage:[UIImage imageNamed:@"上传.png"] forState:UIControlStateNormal];
    [shangchuanButton setShowsTouchWhenHighlighted:1];
    [shangchuanButton setUserInteractionEnabled:0];
    [blurView addSubview:shangchuanButton];
    //上传label
    UILabel *shangchuanLabel = [[UILabel alloc] initWithFrame:CGRectMake(160 * KUAN, 150 * GAO, 70 * KUAN, 25 * GAO)];
    shangchuanLabel.font = [UIFont systemFontOfSize:13];
    shangchuanLabel.textColor = [UIColor whiteColor];
    
    shangchuanLabel.text = [NSString stringWithFormat:@"无法上传"];
    [blurView addSubview:shangchuanLabel];
    //下载button
    UIButton *xiazaiButton = [[UIButton alloc] initWithFrame:CGRectMake(190 * KUAN, 150 * GAO, 25 * KUAN, 25 * GAO)];
    [xiazaiButton setBackgroundImage:[UIImage imageNamed:@"下载1.png"] forState:UIControlStateNormal];
    //    [xiazaiButton addTarget:self action:@selector(xiazai:) forControlEvents:UIControlEventTouchUpInside];
    //xiazaiButton.backgroundColor = [UIColor redColor];
    //    [ setShowsTouchWhenHighlighted:1];
    //下载Label
    UILabel *xiazaiLabel = [[UILabel alloc] initWithFrame:CGRectMake(230 * KUAN, 150 * GAO, 35 * KUAN, 25 *GAO)];
    xiazaiLabel.font = [UIFont systemFontOfSize:13];
    xiazaiLabel.textColor = [UIColor whiteColor];
    xiazaiLabel.text = [NSString stringWithFormat:@"缓存"];
    //    [blurView addSubview:xiazaiLabel];
    
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"back.png" target:self action:@selector(leftMenuClick:)];
    
    
    //初始化控件
    
//        self.modle1111 = [[DsErErModeler alloc]init];
    titleLabel.text = _modle1111.sanModel.title;
    kindLabel.text = _modle1111.sanModel.category;
    timeLabel.text = [self getTime:[_modle1111.sanModel.duration integerValue]];
    detailLabel.text = _modle1111.sanModel.descriptionn;
    //裁剪的小View
    UIImageView *cutView = [[UIImageView alloc]init];
    [cutView sd_setImageWithURL:[NSURL URLWithString:_modle1111.sanModel.siModel.sharing]];
    
    [blurimage sd_setImageWithURL:[NSURL URLWithString:_modle1111.sanModel.siModel.blurred]];
    self.cutFrame = MVview.frame;
    self.cent = MVview.center;
    cutView = [self imageWithImageView:cutView scaledToSize:self.cutFrame.size];
    CGPoint poin = CGPointMake(MVview.center.x, MVview.frame.origin.y + cutView.frame.size.height/2 - 64 );
    cutView.center = poin;
//    [MVview addSubview:cutView];
    
    [MVview sd_setImageWithURL: [NSURL URLWithString:_modle1111.sanModel.siModel.detail] ];
    
    
    [blurView addSubview:detailLabel];
    //    [blurView addSubview:xiazaiButton];
    
    
}
-(void)shoucanButtonDidClicked:(UIButton *)sender
{
    [sender setShowsTouchWhenHighlighted:1];
    DakuKuLei *daku = [[DakuKuLei alloc] init];
    
    daku.tupian =_modle1111.sanModel.siModel.detail;
    daku.mingzi =_modle1111.sanModel.title;
    daku.shichang = [_modle1111.sanModel.duration stringValue];
    
    if (!_isShoucang) {
 
        _shoucangLabel.text = [NSString stringWithFormat:@"已成功"];
        _shoucangImage = [UIImage imageNamed:@"已收藏"];
        [_shoucangButton setBackgroundImage:_shoucangImage forState:UIControlStateNormal];
        _isShoucang = 1;
        /**<  收重复从夫 */
        NSMutableArray *arr = [[DataKu shareInstance] selet];
        for (DakuKuLei * r  in arr) {
            if ([r.mingzi isEqualToString:daku.mingzi]) {
                return;
                
            }
        }
        [[DataKu shareInstance] insertWangzhi:daku];
        
        
        
    }else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLogin"];
        
        [[DataKu shareInstance] deleteNumber:daku.mingzi];
  

        _shoucangLabel.text = [NSString stringWithFormat:@"未收藏"];
        _shoucangImage = [UIImage imageNamed:@"未收藏"];
        [_shoucangButton setBackgroundImage:_shoucangImage forState:UIControlStateNormal];
        _isShoucang = 0;
    }
        NSLog(@"点击了");
    
}
/**<  强制竖屏 */
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(void)bofangButtonBackGroundHighlighted:(UIButton *)sender{
    MovieViewController *mo = [[MovieViewController alloc]init];
    mo.string = _modle1111.sanModel.playUrl;
    mo.name = _modle1111.sanModel.title;
    [self presentViewController:mo animated:YES completion:^{
        
        
    }];
  
    
}

- (UIImageView*)imageWithImageView:(UIImageView*)imageView
                      scaledToSize:(CGSize)newSize;
{
    UIImage *image = imageView.image;
    UIGraphicsBeginImageContext( newSize );
    
    [image drawInRect:CGRectMake(-self.cent.x/2,-self.cent.y/3 + 40,newSize.width + screenFrame.size.width / 2  ,newSize.height + screenFrame.size.height/ 3)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIImageView *newView = [[UIImageView alloc]initWithImage:newImage];
    UIGraphicsEndImageContext();
    
    return newView;
}
-(void)leftMenuClick:(id)sender{
    [self.navigationController popViewControllerAnimated:1];
}
-(NSString *)getTime:(NSInteger)time {
    NSInteger min;
    NSInteger sec;
    min = time / 60;
    sec = time % 60;
    return [NSString stringWithFormat:@"%.2ld'%.2ld\"",min,sec];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
